<template>
  <el-card>
    <template #header> 个人中心 </template>
    <div class="Mine-all">
      狠人阿凝
      <ul>
        <li>
          <span>用户头像：</span>
          <p class="tx">
            <img :src="users.img_url" alt="图片正在加载" />
          </p>
          <el-upload
            class="avatar-uploader"
            action="http://wxopen.besthuishenghuo.com/api/file/pictureUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </li>
        <li>
          <span>用户ID：</span>
          <p>{{ users.member_id }}</p>
        </li>
        <li>
          <span>用户名称：</span>
          <p>{{ users.username }}</p>
        </li>
        <li>
          <span>用户昵称：</span>
          <p>{{ users.nickname }}</p>
        </li>
        <!-- <li>
          <span>账号权限：</span>
          <p>{{ users.group_id }}</p>
        </li> -->
        <li>
          <span>创建时间：</span>
          <p>{{ users.create_time }}</p>
        </li>
        <li class="bc">
          <el-button type="primary" @click="bcAvatar">保存</el-button>
        </li>
      </ul>
    </div>
  </el-card>
</template>

<script>
import { xgAvatar } from '@/api/users.js'
export default {
  data() {
    return {
      users: {},
      imageUrl: '',
      avatarUrl: ''
    }
  },
  created() {
    // 获取本地数据的 token 和 member_id
    this.users = JSON.parse(localStorage.getItem('users'))
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // 处理前端本地预览的功能，并不是实质性上传图片
      this.imageUrl = URL.createObjectURL(file.raw)
      // 把响应回来的内容，主要是图片的 id 储存起来，方便下一次请求
      this.avatarUrl = res.data.id
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    async bcAvatar() {
      if (this.avatarUrl === '') return ''
      const { code, msg } = await xgAvatar({
        member_id: this.users.member_id,
        access_token: this.users.access_token,
        head_img_id: this.avatarUrl
      })
      if (code === 1) {
        this.$message.success(msg)
        // location.reload() // 刷新
      } else {
        this.$message.error(msg)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.Mine-all {
  ul {
    margin: 0;
    padding: 0;
    li {
      display: flex;
      align-items: center;
      margin: 10px 0;
      &.bc {
        padding-top: 20px;
        padding-left: 20px;
      }
      span {
        width: 100px;
        text-align: right;
        margin-right: 10px;
      }
      .tx {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        // border: 2px solid #f4f4f4;
        box-shadow: 0 0 4px;
        // background-color: #ccc;
        display: flex;
        justify-content: center;
        align-items: center;
        overflow: hidden;
        margin-right: 60px;
        img {
          width: 100%;
          //   transform: scale(1.2);
        }
      }
      .avatar-uploader {
        border: 1px dashed #d9d9d9;
        border-radius: 50%;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
      .avatar-uploader:hover {
        border-color: #409eff;
      }
      .avatar {
        width: 80px;
        height: 80px;
        display: block;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 80px;
        height: 80px;
        line-height: 80px;
        text-align: center;
      }
    }
  }
}
</style>
